//引入创建路由createRouter、路由模式createWebHistory的方法
import { createRouter, createWebHistory } from 'vue-router'
//引入HomeView.vue这个文件
import HomeView from '../views/HomeView.vue'

//引入登录信息仓库
import { useLoginStore } from '@/stores/login'

//创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),//创建路由模式History实例
  routes: [//路由的配置
    {
      path: '/',//路由的路径 - 浏览器中项目启动的IP地址后面的路径
      name: 'home',//路由的名称
      meta:{//路由的元信息
        title:"首页",//标题
        requiresAuth:true //状态
      },
      component: HomeView//组件指向到哪个组件（HomeView）第一种写法
    },
    {
      path: '/about',
      name: 'about',
      meta:{
        title:"关于",
        requiresAuth:true
      },
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    },
    {
      path:'/my',
      name:'my',
      meta:{
        title:"我的",
        requiresAuth:true
      },
      component:()=>import('../views/MyView.vue') //第二种写法
    },
    {
      path:'/method',
      name:'method',
      meta:{
        title:"属性",
        requiresAuth:true
      },
      component:()=>import('../views/MethodView.vue')
    },{
      path:'/com',
      name:'com',
      meta:{
        title:"页面级组件",
        requiresAuth:true
      },
      component:() => import('../views/ComView.vue')
    },{
      path:'/parent',
      name:'parent',
      meta:{
        title:"顶级组件",
        requiresAuth:true
      },
      component:()=>import('../views/ParentView.vue')
    },{
      path:'/page',
      name:'page',
      meta:{
        title:"卧龙控股",
        requiresAuth:false
      },
      component:()=>import('../views/PageView.vue')
    },{
      path:'/login',
      name:'login',
      meta:{
        title:"登录",
        requiresAuth:false
      },
      component:()=>import('../views/LoginView.vue')
    }
  ]
})

//路由前置守卫
//一般用于导航守卫，检测用户是否登录，如果没有登录则默认跳转登录页进行登录
//to:即将要去的路由 from:当前要离开的路由 next:中间件，执行下一步函数操作
router.beforeEach((to,from,next)=>{

  const loginStore = useLoginStore()

  var boo = loginStore.loginBoolean   //true:已经登录 false：未登录
  if(boo){
    next()
  }else{
    // next("/my")
    if(to.path == "/login"){
      next()
      return
    }else{
      next("/login")
    }
  }
})

//默认暴露出路由对象
export default router
